package p.jce.cert;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CertificateFactorySpi;

import p.jce.BC;

public class RCertificateFactory {
	private static final class X extends CertificateFactory {

		protected X(CertificateFactorySpi certFacSpi, Provider provider, String type) {
			super(certFacSpi, provider, type);

		}
	}

	public static CertificateFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException,
			CertificateException {
		return getInstance(algorithm);
	}

	public static CertificateFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException,
			NoSuchProviderException, CertificateException {
		if ("BC".equals(provider)) {
			try {
				return new X((CertificateFactorySpi) BC.newInstance("CertificateFactory", algorithm), null, algorithm);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return CertificateFactory.getInstance(algorithm, provider);
	}

	public static CertificateFactory getInstance(String algorithm) throws NoSuchAlgorithmException,
			CertificateException {
		try {
			return getInstance(algorithm, "BC");
		} catch (NoSuchProviderException e) {
			throw new RuntimeException(e);
		}
	}
}
